package distriKub;

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class GameListener extends Thread{
	public static final String COMTERMINATION = "EndOfTransmission";

	// Start of making ComListener an event generator
	private List<MessageListener> _listeners = new ArrayList<MessageListener>();

	public synchronized void addMessageListener(MessageListener l) {
		_listeners.add(l);
	} // End of addMessageListener

	public synchronized void removeMessageListener(MessageListener l) {
		_listeners.remove(l);
	} // End of removeMessageListener

	private synchronized void _fireComEvent(String message, InetAddress sourceIp) {
		ComEvent messageEvent = new ComEvent(this, message);
		Iterator<MessageListener> listeners = _listeners.iterator();
		while (listeners.hasNext()) {
			((MessageListener) listeners.next()).messageReceived(messageEvent);
		} // End of while block
	} // End of _fireComEvent

	// End of making ComListener an event generator

	
	public GameListener(int tcpPort){ // Constructor
        try {
            serverSocket = new ServerSocket(tcpPort);
         }
         catch (IOException e) {
            System.out.println(e);
         }
	}

	        ServerSocket serverSocket = null;
	        String line;
	        BufferedReader is;
	        PrintStream os;
	        Socket clientSocket = null;
	        boolean listening = true;
	        
	        public void run(){
	        	// Create a socket object from the ServerSocket to listen and accept connections.
	        	// Open input and output streams
	        	while (listening) {
	        	        try {
	        	           try{clientSocket = serverSocket.accept();} // Could throw an exception if serverSocket was closed from outside the class
	        	           catch (IOException IOEX){System.out.println("\nNo more listening to TCP ==> GameListener.java\n"); return;}
	        	           is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
	        	           os = new PrintStream(clientSocket.getOutputStream());
	        	           
	        	           InetAddress sourceIp = clientSocket.getInetAddress();
	        	           
	        	             line = is.readLine();
	        	             System.out.println("===> GameListener.java ===> Got Message : " + line);
	        	             if (line.equals(ComRules.comTerminationString))
	        	            	 listening = false;
	        	             _fireComEvent(line,sourceIp);
	        	             os.println(line.hashCode()); 
	        	           }
	        	           
	        	        catch (IOException e) {
	        	           System.out.println(e);
	        	        }
	        	}
	        	    }
	        
	        public ServerSocket getSocket(){
	        	return serverSocket;
	        }
	        
	        public void closeGameListener(){
	        	listening = false;
	        	if (os!=null) os.close();
	        	try{
	        	if (is!=null) is.close();
	        	if (clientSocket!=null) clientSocket.close();
	        	} catch (Exception EX){
	        		System.out.println("\n===> GameListener.java ===> Error closing the socket\n");
	        		EX.printStackTrace();
	        	}
	        }

}
